package server.network;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;

import server.config.Public;
import server.views.ServerMain;

/**
 * 服务器端线程，用来接受来自客户端的连接
 * 
 * @author solomon
 * 
 */
public class ServerThread implements Runnable {
	private boolean flag;

	private ServerMain f;

	public ServerThread(boolean flag, ServerMain f) {
		super();
		this.flag = flag;
		this.f = f;
	}

	public void run() {
		// 创建服务器端ServerSocket
		while (!flag) {
			try {
				// 监听来自客户端程序的请求，当收到请求时创建一个响应请求的Socket对象
				Socket socket = Public.serverSocket.accept();
				// 创建一个线程来处理该连接,验证线程
				ValidateThread validateThread = new ValidateThread(socket, f);
				Thread t1 = new Thread(validateThread);
				t1.start();
				Thread.sleep(100);// 让当前线程休息
			} catch (SocketException e1) {
				return;
			} catch (IOException e1) {
				e1.printStackTrace();
				return;
			} catch (InterruptedException e) {
				e.printStackTrace();
				return;
			}
		}
	}

}
